import requests
import json
import re
import xlwt
url = 'https://club.jd.com/comment/productPageComments.action?callback=fetchJSON_comment98&productId=10025836530937&score=0&sortType=5&page=0&pageSize=10&isShadowSku=0&fold=1'
# 搜索网址
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36'}  # 伪装
# 伪装爬虫
r = requests.get(url, headers=headers)  # 请求网站
body = r.text
list = re.findall(r"fetchJSON_comment98\((.*?)\)", body)
print(list)
data = json.loads(list[0])
# 创建一个workbook 设置编码
workbook = xlwt.Workbook(encoding = 'utf-8')
# 创建一个worksheet
worksheet = workbook.add_sheet('My Worksheet')
worksheet.write(0,0, label = 'id')
worksheet.write(0,1, label = '评论')
worksheet.write(0,2, label = '时间')
worksheet.write(0,3, label = '产品颜色')
worksheet.write(0,4, label = '产品型号')
content = data['comments']
for i in range(len(content)):
    worksheet.write(i+1, 0, label=content[i]['id'])
    worksheet.write(i+1, 1, label=content[i]['content'])
    worksheet.write(i+1, 2, label=content[i]['creationTime'])
    worksheet.write(i+1, 3, label=content[i]['productColor'])
    worksheet.write(i+1, 4, label=content[i]['productSize'])
workbook.save('jd_comments.xls')

